﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Tesla.Service.DataContracts;
using Tesla.DataAccess.DataAccessComponents;
using Tesla.Util.Utilities;
using Tesla.DataAccess.DataAccessLogic.Interfaces;

namespace Tesla.DataAccess.DataAccessLogic.Implementations
{
    public class PhaseDA : BaseOperationalDA, IPhaseDA
    {
        #region BaseOperationalDA Members
        public override int? Create(BaseDC bDC)
        {
            int success = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;

                    success = context.usp_CreatePhase(
                                                SetSafeString(((PhaseDC)bDC).PhaseCode),
                                                SetSafeInt(((PhaseDC)bDC).ProjectID),
                                                SetSafeString(((PhaseDC)bDC).PhaseShortDescription),
                                                (((PhaseDC)bDC).PhaseCompletionPct));


                    context.SubmitChanges();
                    return success;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override int Update(BaseDC bDC)
        {
            int recordCount = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    recordCount = context.usp_UpdatePhase(
                                                SetSafeInt(((PhaseDC)bDC).PhaseID),
                                                SetSafeString(((PhaseDC)bDC).PhaseCode),
                                                SetSafeInt(((PhaseDC)bDC).ProjectID),
                                                SetSafeString(((PhaseDC)bDC).PhaseShortDescription),
                                                (((PhaseDC)bDC).PhaseCompletionPct));
                    context.SubmitChanges();
                    return recordCount;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> DisplayAll(int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> phases = null;
            try
            {

                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_DisplayAllPhaseResult> phaseList = context.usp_DisplayAllPhase(gridSize, ref totalRecordCount).Cast<usp_DisplayAllPhaseResult>();
                    phases = GenericConversion.ConvertToTargetType(phaseList.ToList<usp_DisplayAllPhaseResult>().ToArray(), typeof(usp_DisplayAllPhaseResult), typeof(PhaseDC));

                }

                return phases;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> SearchByName(string name, int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> phases = null;

            try
            {

                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_SearchPhaseByNameResult> phaseList = context.usp_SearchPhaseByName(name, gridSize, ref totalRecordCount).Cast<usp_SearchPhaseByNameResult>();
                    phases = GenericConversion.ConvertToTargetType(phaseList.ToList<usp_SearchPhaseByNameResult>().ToArray(), typeof(usp_SearchPhaseByNameResult), typeof(PhaseDC));
                }
                return phases;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> Read(int id)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadPhaseResult> projectList = context.usp_ReadPhase(id).Cast<usp_ReadPhaseResult>();
                    return GenericConversion.ConvertToTargetType(projectList.ToList<usp_ReadPhaseResult>().ToArray(), typeof(usp_ReadPhaseResult), typeof(PhaseDC));                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> ReadByRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadPhaseByRowNumberRangeResult> phaseList = context.usp_ReadPhaseByRowNumberRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString).Cast<usp_ReadPhaseByRowNumberRangeResult>();
                    return GenericConversion.ConvertToTargetType(phaseList.ToList<usp_ReadPhaseByRowNumberRangeResult>().ToArray(), typeof(usp_ReadPhaseByRowNumberRangeResult), typeof(PhaseDC));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        public IList<BaseDC> ReadAllProjects()
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadAllProjectsResult> projectList = context.usp_ReadAllProjects().Cast<usp_ReadAllProjectsResult>();
                    return GenericConversion.ConvertToTargetType(projectList.ToList<usp_ReadAllProjectsResult>().ToArray(), typeof(usp_ReadAllProjectsResult), typeof(ProjectDC));

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
